package cn.tedu.ivos.audit.service.impl;


import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditQuery;
import cn.tedu.ivos.audit.pojo.dto.AuditSaveParam;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class AuditServiceImpl implements AuditService {

    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApplicationMapper applicationMapper;

    /**
     * 审批单
     * @param application
     */
    @Override
    public void insert(Application application) {
        List<Long> auditUserIdList = application.getAuditUserIdList();
        for (int i = 0; i < auditUserIdList.size(); i++){
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(auditUserIdList.get(i));
            if(i==0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENGDING.getCode());
            }else {
                audit.setAuditStatus(AuditStatusEnum.PENGDING.getCode());
            }
            audit.setAuditSort(i);
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> select(AuditQuery query) {
        List<AuditVO> list =  auditMapper.select(query);
        for (AuditVO auditVO : list) {
            //存用户id集合
            ArrayList<Long> auditUserIdList = new ArrayList<>();
            //存用户名集合
            ArrayList<String> auditUsernameList = new ArrayList<>();
            List<Audit> audits = auditMapper.selectByApplicationId(auditVO.getApplicationId());
            for (Audit audit : audits) {
                auditUserIdList.add(audit.getAuditUserId());
                UserVO userVO = userMapper.selectById(audit.getAuditUserId());
                auditUsernameList.add(userVO.getUsername());
            }
            StringJoiner stringJoiner = new StringJoiner(",");
            for (String username:auditUsernameList){
                stringJoiner.add(username);
            }
            auditVO.setAuditUsernameList(stringJoiner.toString());
            auditVO.setAuditUserIdList(auditUserIdList);
        }
        return list;
    }

    /**
     * 审批
     * @param param
     */
    @Override
    public void update(AuditSaveParam param) {
        Audit audit = new Audit();
        BeanUtils.copyProperties(param, audit);
        audit.setUpdateTime(new Date());
        //根据id修改审批单的状态和更新时间
        auditMapper.update(audit);
        Application app = new Application();
        app.setId(param.getApplicationId());
        app.setUpdateTime(new Date());
        if(audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //根据申请单id查找没有审批的数量
          Long id =  param.getApplicationId();//申请单id
            //查出个数
            int count=auditMapper.selectByCount(id);
          log.debug("{}--------------",count);
          if (count>0){
              AuditQuery auditQuery = new AuditQuery();
              auditQuery.setApplicationId(id);
              auditQuery.setAuditSort(1);
              List<AuditVO> auditList = auditMapper.select(auditQuery);
              if(auditList.size()>0 && auditList !=null){
                  AuditVO auditVO = auditList.get(0);
                  Audit audit1 = new Audit();
                  audit1.setId(auditVO.getId());
                  audit1.setAuditStatus(AuditStatusEnum.MY_PENGDING.getCode());
                  audit1.setUpdateTime(new Date());
                  auditMapper.update(audit1);
              }
              app.setStatus(ApplicationStatusEnum.AUDIT.getCode());
              applicationMapper.updata(app);
          }else{
              app.setStatus(ApplicationStatusEnum.AUDITED.getCode());
              applicationMapper.updata(app);
          }
        }else {
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(param.getApplicationId());
            List<AuditVO> select = auditMapper.select(auditQuery);
            if (select.size()>0 && select !=null){
                for (AuditVO auditVO : select) {
                    if (auditVO.getAuditStatus().equals(AuditStatusEnum.PENGDING.getCode())) {
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }
            app.setStatus(ApplicationStatusEnum.REJECT.getCode());
            app.setRejectReason(param.getRejectReason());
            applicationMapper.updata(app);
        }
    }
}
